package com.manager.action.message;

import java.io.IOException;
import java.io.Reader;
import java.sql.Clob;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import org.common.tool.FillBeanUtils;
import org.common.util.TranslateHelper;
import org.wface.web.core.ActionSupport;
   
import com.manager.service.message.MessageBackService;
import com.manager.service.message.MessageService;
import com.web.model.Message; 
import com.web.model.MessageBack;
import com.web.model.vo.query.MessageQuery;
import com.web.tag.bean.PageModel;

public class MessageAction extends ActionSupport {

	private String id;
	private String page;

	private String classid;
	private String title;

	public String list() {
		PageModel model = new PageModel();
		int pageNum = TranslateHelper.parseInteger(page);
		model.setPageNum(pageNum);
        
		MessageQuery query=new MessageQuery();
		MessageService messageService = new MessageService();
		List<Message> messages = messageService.findList(model, query);
		getRequest().setAttribute("messages", messages);  
		model.setPaths(getBasePath() + "/manager/message/list.do?");
		getRequest().setAttribute(PageModel.PAGEMODEL, model);
		return "list";
	}

	private String getBasePath() {
		String basePath = getRequest().getScheme() + "://" + getRequest().getServerName() + ":" + getRequest().getServerPort() + getRequest().getContextPath();
		return basePath;
	}

	public String create() { 
		return "create";
	}

	 

	public String edit() {
		MessageService messageService = new MessageService();
		Message obj=messageService.getById(TranslateHelper.parseInteger(id));
		getRequest().setAttribute("news", obj); 
		return create();
	}

	public String delete() {
		MessageService messageService = new MessageService();
		messageService.delete(TranslateHelper.parseInteger(id));
		return "!/manager/messages/list.do";
	}
    
	public String backMsg(){
		MessageService messageService = new MessageService();
		MessageBackService messageBackService=new MessageBackService();
		Message obj=messageService.getById(TranslateHelper.parseInteger(id));
		List<MessageBack> list= messageBackService.findListByMsg(TranslateHelper.parseInteger(id));
		
		
		getRequest().setAttribute("messages", obj); 
		getRequest().setAttribute("messagebks", list); 
		return "show";
	}
	
	public String backMsgSave(){
		MessageService messageService = new MessageService();
		
		Message obj=messageService.getById(TranslateHelper.parseInteger(id));
		if(getTime(getRequest().getParameter("addtime"))!=null){
			obj.setAddtime(getTime(getRequest().getParameter("addtime")));
			}
		getRequest().setAttribute("news", obj); 
		return "list";
	}
	public String show(){
		MessageService messageService = new MessageService();
		
		//Message obj=messageService.findList(model, query)(TranslateHelper.parseInteger(id));
		getRequest().setAttribute("message", ""); 
		return "backMsg";
	}
	public void setId(String id) {
		this.id = id;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public void setClassid(String classid) {
		this.classid = classid;
	}

	public void setTitle(String title) {
		this.title = title;
	}
	public Timestamp getTime(String str_test){
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		  format.setLenient(false);
		  //要转换字符串 str_test
		  Timestamp ts =null;
		  try {
		   ts = new Timestamp(format.parse(str_test).getTime());
		   System.out.println(ts.toString());
		  } catch (ParseException e) {
		   // TODO Auto-generated catch block
		   e.printStackTrace();
		  }
		  return ts;
		  
	}
}
